/* Launch the game, etc. */

private {
	import dwt.all;
	import main;
	import common;
	import settings;
	import dialogs;
}

version (Windows) {
	private PROCESS_INFORMATION *info = null;
}

void JoinServer(char[] ip)
{
	char[] argv;
	char[] path = getSetting("gamePath");
	char[] dir = getDirName(path);

	version (Windows) {
		STARTUPINFOA startup;
	}

	if (MOD_ONLY)
		argv = "+set fs_game " ~ modName;
	argv ~= " +connect " ~ ip;

	JoinDialog dialog = new JoinDialog(mainWindow, "Command line:\n" ~ path ~ " " ~ argv);
	int res = dialog.open();
	if (res == DWT.OK && dialog.password.length) {
		argv ~= " +set password " ~ dialog.password;
	}

	if (res == DWT.OK) {
		version (Windows) {
			GetStartupInfoA(&startup);
			startup.dwFlags = STARTF_USESTDHANDLES;
			info = new PROCESS_INFORMATION();

			int r = CreateProcessA(null, toStringz(path ~ " " ~ argv),
			               null, null, true, 0/*DETACHED_PROCESS*/, null,toStringz(dir),&startup,info);

			if (!r) {
				int e = GetLastError();
				db("CreatProcessA returned " ~ std.string.toString(r));
				db("GetLastError returned " ~ std.string.toString(e));
			}
			else if (getSetting("minimizeOnGameLaunch") == "true") {
				mainWindow.setMinimized(true);
			}
		}
		else {
			assert(0);
		}
	}
}
